package psn.hui.bus.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import psn.hui.bus.domain.Customer;
import psn.hui.bus.domain.Rent;
import psn.hui.bus.service.CustomerService;
import psn.hui.bus.service.RentService;
import psn.hui.bus.vo.RentVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.User;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;
import psn.hui.sys.utils.RandomUtil;
import psn.hui.sys.utils.WebUtils;

/**
 * 	汽车出租管理控制器
 * @author hui
 *
 */
@RestController
@RequestMapping("rent")
public class RentController {
	@Autowired
	private RentService rentService;
	
	@Autowired
	private CustomerService customerService;
	
	/**
	 * 	检查该身份证号的客户是否存在
	 * @return
	 */
	@RequestMapping("checkCustomerExist")
	public JsonResult checkCustomerExist(Customer customer) {
		try {
			boolean notExist = customerService.checkIdentity(customer.getIdentity());
			if(!notExist) {
				return SysConstant.CUSTOMER_IDENTITY_EXIST_TRUE;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.CUSTOMER_IDENTITY_EXIST_FALSE;
	}
	
	/**
	 * 初始化出租单信息
	 * @param rent
	 * @return
	 */
	@RequestMapping("initRentForm")
	public DataGridView initRentForm(Rent rent) {
		DataGridView dataGridView = new DataGridView();
		dataGridView.setCode(1);
		try {
			//生成出租单号
			rent.setRentid(RandomUtil.createRandomStringUseTime(SysConstant.RENTID_SUFFIX));
			//设置起租时间
			rent.setBegindate(new Date());
			//设置操作员
			User user = (User)WebUtils.getHttpSession().getAttribute("user");
			rent.setOpername(user.getRealname());
			dataGridView.setCode(0);
			dataGridView.setData(rent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataGridView;
	}
	
	/**
	 * 	添加出租单
	 * @param rent
	 * @return
	 */
	@RequestMapping("addRent")
	public JsonResult addRent(Rent rent) {
		try {
			//设置创建时间
			rent.setCreatetime(new Date());
			//设置归还状态
			rent.setRentflag(SysConstant.RENT_FLAG_RENTING);
			if(rentService.addRent(rent)>0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}
	
	/**
	 * 修改出租单
	 * @param rent
	 * @return
	 */
	@RequestMapping("updateRent")
	public JsonResult updateRent(Rent rent) {
		try {
			if(rentService.updateRent(rent)>0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}
	/**
	 * 删除出租单
	 * @param rent
	 * @return
	 */
	@RequestMapping("deleteRent")
	public JsonResult deleteRent(Rent rent) {
		try {
			if(rentService.deleteRent(rent.getRentid())>0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	/**
	 * 	查询出租单
	 * @param rentVo
	 * @return
	 */
	@RequestMapping("rentList")
	public DataGridView rentList(RentVo rentVo) {
		try {
			return rentService.queryAllRent(rentVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DataGridView();
	}
	
}
